package com.huiquan.icd.job;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.huiquan.foundation.constant.BusinessContants;
import com.huiquan.foundation.util.BusinessUtil;
import com.huiquan.framework.dao.EmailDao;
import com.huiquan.framework.utils.DateUtils;
import com.huiquan.framework.utils.EMailUtil;
import com.huiquan.icd.dao.IcdDiseaseIndexDao;
import com.huiquan.icd.service.IcdDiseaseSectionRepositoryService;

/**
 * Description: 如果对疾病表有修改，则将疾病的检验类别和黑名单导出到文件
 */
@Component
public class IcdDiseaseJob {

	private static final Logger LOG = LoggerFactory.getLogger(IcdDiseaseJob.class);

	@Autowired
	private IcdDiseaseIndexDao icdDiseaseIndexDao;
	@Autowired
	private IcdDiseaseSectionRepositoryService icdDiseaseSectionRepositoryService;
	@Autowired
	private EmailDao emailDao;

	@Value("${di.section.repository.script}")
	private String diSectionRepositoryScript;

	public void generateDiseaseSectionRepository() throws Exception {
		LOG.info("generateDiseaseSectionRepository begin ");

		// 调用脚本同步数据
		BusinessUtil.excuteShell(new String[] { diSectionRepositoryScript });
		LOG.info("generateDiseaseSectionRepository end  !");

		LOG.info("sectionRepository init begin");

		icdDiseaseSectionRepositoryService.init();
		LOG.info("sectionRepository init end");

	}

	/**
	 * 从hive中获取疾病指标新数据
	 */
	@Value("${di.inspection.init.script}")
	private String diInspectionInitScript;
	@Autowired
	private EMailUtil eMailUtil;

	public void initDiseaseInspection() throws Exception {
		LOG.info("initDiseaseInspection begin!");

		// 未校验的数据不需要累加，在添加之前将所有未校验的都删除
		icdDiseaseIndexDao.deleteUnchecked();

		// 调用脚本初始化数据
		try {
			BusinessUtil.excuteShell(new String[] { diInspectionInitScript, DateUtils.convertTodayFormat() });
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 将数据初始化并返回是否有新数据添加
		try {
			icdDiseaseIndexDao.init();
			int count = icdDiseaseIndexDao.initCount();
			if (count > 0) {
				List<String> addresses = emailDao
						.retrieveAddressList(BusinessContants.SEND_EMAIL_GROUP_DISEASE_INSPECTION_INIT);
				if (!addresses.isEmpty()) {
					eMailUtil.sendBatchEmail("", "疾病指标黑白名单数据更新", addresses);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		LOG.info("initDiseaseInspection end!");
	}
	
	
	/**
	 * 从hive中获取疾病指标已校验的概率新数据
	 */
	@Value("${di.inspection.init.checked.script}")
	private String diInspectionCheckedScript;
	public void initDiseaseInspectionChecked() throws Exception {
		LOG.info("initDiseaseInspectionChecked begin!");

		// 调用脚本初始化数据
		try {
			BusinessUtil.excuteShell(new String[] { diInspectionCheckedScript, DateUtils.convertTodayFormat() });
		} catch (Exception e) {
			e.printStackTrace();
		}

		LOG.info("initDiseaseInspectionChecked end!");
	}
	
	
	
}
